"use strict";

angular.module('app.sensor').controller('AlertsiteCtrl', function ($scope,$cookieStore,$http) {

    //配置分页基本参数
    $scope.paginationConf = {
        currentPage: 1,
        itemsPerPage: 10,
        pagesLength: 5,
        totalItems: 20,
        perPageOptions: [10, 20, 30, 40, 50],
        onChange: function () {
        }
    };
    $scope.fid = $cookieStore.get('userinfo').fid;
    $scope.userid = $cookieStore.get('userid');
    $scope.poc = {};
    $scope.typof = {};
    $scope.insert = {};
    $scope.All_device = null;
    // 选择设备获取设备下报警设置
    $scope.sele_pos = function (deviceid) {
        $scope.select(deviceid);
    }
    // 获取通知类型
    $http.get(baseurl+'/public_types/notice_type').then(function (res) {
        if(res.data.ret === 1){
            $scope.call = res.data.info;
            var arr = res.data.info;
            for (var q = 0;q < arr.length; q++ ){
                var obj = arr[q];
                $scope.typof[obj.id] = obj.name;
            }
            $scope.first_notice_type = res.data.info[0].id;
        }
    });
   // 报警类型
    $http.get(baseurl + '/public_types/warn_type').then(function (res) {
        if(res.data.ret === 1){
            $scope.caveat = res.data.info;
            var arr = res.data.info;
            for (var q = 0;q < arr.length; q++ ){
                var obj = arr[q];
                $scope.poc[obj.id] = obj.name;
            }
            $scope.first_warn_type = res.data.info[0].id;
        }
    });
    //获取设备号
    $http.get(baseurl+'/select_deviceid').then(function(res){
        if (res.data.ret === 1 &&  res.data.info.length > 0) {
            for(var i = 0; i < res.data.info.length; i++){
                res.data.info[i].id += '';
            }
            $scope.alldevice = res.data.info;

            if ($scope.fid == 1) {
                $scope.fid = $scope.userid;
            }
            $http.get(baseurl + '/detail/device?deviceid=' + $scope.alldevice[0].deviceid + '&fid=' + $scope.fid).then(function (res) {
             
                $http.get(baseurl + '/table_columns/log_' + res.data.info.port).then(function (res) {
                    if (res.data.ret == 1) {
                        $scope.All_device = res.data.info;
                        // $scope.polices = res.data.info;
                        $scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', $scope.select($scope.alldevice[0].deviceid));
                    }
                });
            });
            // $scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', $scope.select($scope.alldevice[0].deviceid));
           // $scope.select($scope.alldevice[0].deviceid)
            $scope.insert.deviceid = $scope.alldevice[0].deviceid + '';
            $scope.first_deviceid = res.data.info[0].deviceid;
        }
    });
    $scope.select = function (deviceid) {
        var page_obj = {
            page:$scope.paginationConf.currentPage,
            rows:$scope.paginationConf.itemsPerPage
        };
        $http.post(baseurl + '/list/select/warn_config?deviceid==' + deviceid, page_obj).then(function (res) {
            if (res.data.ret === 1) {
                var arr = res.data.rows;
                for (var i = 0; i < arr.length; i++) {
                    var obj = arr[i];
                    var time = obj.time;
                    obj.time = moment(time).format("YYYY-MM-DD HH:mm:ss");
                    if (obj.time === 'Invalid date') {
                            obj.time = time;
                    }
                    obj.warn_type_name = $scope.poc[obj.warn_type];
                    obj.notice_type_name = $scope.typof[obj.notice_type];
                    
                   /* for (var q = 0; q < $scope.All_device.length; q++){
                        if (arr[i].fieldname == $scope.All_device[q].COLUMN_NAME) {
                            arr[i].fieldvalue = $scope.All_device[q].column_comment;
                        }
                    }*/
                }
                $scope.datas = arr;
                $scope.paginationConf.totalItems = res.data.records;
            }           
        })
    };
    $scope.add_box = function(){
        $scope.insert = {};
        $scope.insert.deviceid = $scope.first_deviceid;
        $scope.insert.notice_type = $scope.first_notice_type;
        $scope.insert.warn_type = $scope.first_warn_type;
        $scope.insert.warn_grade = '1';
    };
    $scope.insertdata = function () {     
        if ($scope.insert.deviceid == null || $scope.insert.deviceid == '') {
            new Notify('info','设备号不为空'); 
            return;
        }
        if ($scope.insert.fieldname === null || $scope.insert.fieldname === undefined || $scope.insert.fieldname === '') {
            new Notify('info','报警字段不为空');
            return;
        }
        if( $scope.insert.warn_type != '3'){
            if ($scope.insert.value == null || $scope.insert.value == '') {
                new Notify('info','报警值不为空');
                return;
            }
        }
        console.log($scope.insert);
        $http.post(baseurl+'/data/warn_config',$scope.insert).then(function(res){
            if (res.data.ret === 1) {
                 $("#addAlert").modal("hide");
                 $scope.select();
                 new Notify('success','添加成功');
            }else if (res.data.ret !== -4){
                 new Notify('fail','这个设备已有该字段的这种类型报警');
            }
        });
    };

    $scope.updatedata = function () {

        if( $scope.editdata.warn_type !== '3'){
            if ($scope.editdata.value === null || $scope.editdata.value === '') {
                new Notify('info','报警值不为空');
                return;
            }
        }
        if ($scope.editdata.deviceid === null || $scope.editdata.deviceid === '') {
            new Notify('info','设备号不为空'); 
            return;
        }
        if ($scope.editdata.fieldname === null || $scope.editdata.fieldname === '') {
            new Notify('info','报警字段不为空');
            return;
        }
        delete $scope.editdata.id;
        delete $scope.editdata.notice_type_name;
        delete $scope.editdata.warn_type_name;
        $http.put(baseurl+'/data/warn_config?id=='+$scope.editid,$scope.editdata).then(function(res){
            if (res.data.ret === 1) {
                 $("#editAlert").modal("hide");
                 $scope.select();
                 new Notify('success','修改成功');
            }else{
                 new Notify('fail','这个设备已有该字段的这种类型报警');
            }
        });
    };

    $scope.editdata = {};
    $scope.editziduan = true;
    $scope.showEditAlert = function (data) {
        $scope.sele_field(data.deviceid);
        //console.log(typeof data.warn_type,data.warn_type);
        if(data.warn_type === 3){
            $scope.editziduan = false;
            $scope.editdata.value = null;
            $scope.editdata.warn_grade = null
        }
        $scope.editid = data.id;
        data.warn_type += "";
        data.notice_type += "";
        $scope.editdata = data;
        $('#editAlert').modal('show');
    };
    $scope.showDelDialog = function (id) {
        $scope.delid = id;
        $('#showDelDialog').modal('show');
    };
    $scope.deldevice = function () {
        $http.delete(baseurl+'/data/warn_config?id=='+$scope.delid).then(function(res){
                if (res.data.ret === 1) {
                     $('#showDelDialog').modal('hide');
                     $scope.select();
                }
            });
    };
    //选择设备显示端口下的字段
    $scope.sele_field = function (deviceid) {
        if ($scope.fid == 1) {
            $scope.fid = $scope.userid;
        }
        //根据设备id找端口
        $http.get(baseurl + '/detail/device?deviceid=' + deviceid + '&fid=' + $scope.fid).then(function (res) {
            $http.get(baseurl + '/table_columns/log_' + res.data.info.port).then(function (res) {
                if (res.data.ret == 1) {
                    $scope.All_device = res.data.info;
                    // $scope.polices = res.data.info;
                    var arr1 = handle_field(res.data.info)
                    $scope.polices = arr1;
                    //console.log($scope.polices);
                    if (arr1.length > 0) {
                        $scope.insert.fieldname = arr1[0].COLUMN_NAME;
                    }else{
                        $scope.insert.fieldname = '';
                    }
                }
            });
        });
    }
    var handle_field = function (arr) {
        var arr1 = [];
        for (var i = 0; i < arr.length; i++) {
            if (i > 3) {
               arr1.push(arr[i]);
            }
        }
        return arr1;
    }
    // 添加等级报警不显示字段和值
    $scope.ziduan = true;
    $scope.sele_poc = function (num) {
        if(num === '3'){
            $scope.ziduan = false;
            $scope.insert.value = null;
            $scope.insert.warn_grade = null;
        }else {
            $scope.ziduan = true;
        }
    };
    $scope.sele_edit = function (id) {
        if(id === '3'){
            $scope.editziduan = false;
            $scope.editdata.value = null;
            $scope.editdata.warn_grade = null
        }else {
            $scope.editziduan = true;
        }
    };

    
});
